package com.asion.gankapp.view.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.asion.gankapp.R;
import com.asion.gankapp.adapters.BaseRecyclerAdapter;
import com.asion.gankapp.entrys.BaseEntry;
import com.asion.gankapp.entrys.BaseMeituEntry;
import com.asion.gankapp.presenter.SearchPresenter;
import com.asion.gankapp.utils.ThemeChangeUtil;
import com.asion.gankapp.utils.titanic.Titanic;
import com.asion.gankapp.utils.titanic.TitanicTextView;
import com.asion.gankapp.view.BaseView;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener, BaseView<SearchPresenter>, SlidingPaneLayout.PanelSlideListener {

    private ImageView mSearchBack;
    private EditText mSearchContent;
    private ImageView mSearch;
    private Button mSearchChange;
    private SearchPresenter searchPresenter;
    private RecyclerView mSearchRecycler;
    private SlidingPaneLayout mSearchSliding;
    private BaseRecyclerAdapter baseRecyclerAdapter;
    private int page = 1;
    private boolean isLoading;
    private TitanicTextView mMyTextView;
    private LinearLayout mLoding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeChangeUtil.changeTheme(this);
        setContentView(R.layout.activity_search);
        initView();

        mSearchSliding.setPanelSlideListener(this);
        mSearchSliding.setSliderFadeColor(0X00000000);

        baseRecyclerAdapter = new BaseRecyclerAdapter(this);
        baseRecyclerAdapter.setType(1);
        mSearchRecycler.setItemAnimator(new DefaultItemAnimator());
        mSearchRecycler.setLayoutManager(new LinearLayoutManager(this));
        mSearchRecycler.setAdapter(baseRecyclerAdapter);

        baseRecyclerAdapter.setOnItemClickListener((view, position, resultsBean, gank) -> {
            Bundle bundle1 = new Bundle();
            bundle1.putSerializable("resultBean",resultsBean);
            Intent intent = new Intent(SearchActivity.this, WebActivity.class);
            intent.putExtra("bundle", bundle1);
            Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(this, R.anim.in, R.anim.out).toBundle();
            ActivityCompat.startActivityForResult(this, intent,100, bundle);
        });

    }


    @Override
    protected void onResume() {
        super.onResume();
        searchPresenter.start(null, 0);
    }

    private void initView() {
        mSearchBack = (ImageView) findViewById(R.id.search_back);
        mSearchContent = (EditText) findViewById(R.id.search_content);
        mSearch = (ImageView) findViewById(R.id.search);
        mSearchChange = (Button) findViewById(R.id.search_change);
        mSearchRecycler = (RecyclerView) findViewById(R.id.search_recycler);
        mSearchSliding = (SlidingPaneLayout) findViewById(R.id.search_sliding);

        searchPresenter = new SearchPresenter(this);

        mSearchBack.setOnClickListener(this);
        mSearchChange.setOnClickListener(this);
        mSearch.setOnClickListener(this);
        mMyTextView = (TitanicTextView) findViewById(R.id.my_text_view);
        mLoding = (LinearLayout) findViewById(R.id.loding);

        new Titanic().start(mMyTextView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_back:
                finish();
                break;
            case R.id.search_change:
                baseRecyclerAdapter.removeAll();
                searchPresenter.setRandomData();
                break;
            case R.id.search:
                submit();
                hide();
                break;
        }
    }

    private void submit() {
        String content = mSearchContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            Snackbar.make(mSearchSliding, "搜索内容不能为空！", Snackbar.LENGTH_SHORT).show();
        } else {
            mLoding.setVisibility(View.VISIBLE);
            mSearchChange.setVisibility(View.GONE);
            baseRecyclerAdapter.removeAll();
            searchPresenter.setQurryData(content, 1);
            mSearchRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int totalItemCount = layoutManager.getItemCount();
                    int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                    if (!isLoading && totalItemCount < (lastVisibleItem + 8)) {
                        searchPresenter.start(content, ++page);
                        isLoading = true;
                    }
                }
            });
        }
    }

    @Override
    public void onPanelSlide(View panel, float slideOffset) {

    }

    @Override
    public void onPanelOpened(View panel) {
        this.finish();
    }

    @Override
    public void onPanelClosed(View panel) {

    }

    @Override
    public void setPresenter(SearchPresenter presenter) {
        this.searchPresenter = presenter;
    }

    @Override
    public void showAllTypeSuccess(BaseEntry baseEntry) {
        if(baseEntry != null){
            baseRecyclerAdapter.addAll(baseEntry.getResults());
        }
        mLoding.setVisibility(View.GONE);
        isLoading = false;
    }

    @Override
    public void showAllTypeError(String s) {
        Snackbar.make(mSearchSliding, s, Snackbar.LENGTH_SHORT).show();
        isLoading = false;
    }

    @Override
    public void showMeituSuccess(BaseMeituEntry baseMeituEntry) {

    }

    @Override
    public void showMeituError(String s) {

    }

    public void hide(){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mSearchContent.getWindowToken(), 0); //强制隐藏键盘
    }
}
